// plugin/pages/myAuction/myAuction.js
var WxParse = require('../../../wxParse/wxParse.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curStatus:'all',
    showLoading: true,
    noMoretip: false,
    page: 0,
    curTid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    app.setNavtitle('我的竞拍');
    if (e && e.id) {
      that.setData({
        id: e.id
      })
    }
    that.requestOrderlist();
  },
  onShow:function(){
    // var that = this;
    // that.onPullDownRefresh();
  },
  toggleStatus:function(e){
    var that = this;
    var status = e.currentTarget.dataset.status;
    that.setData({
      curStatus:status,
      showLoading: true,
      noMoretip: false,
      page: 0,
      orderList:null
    })
    that.requestOrderlist();
  },
  requestOrderlist: function () {
    var that = this;
    var page = that.data.page;
    var data = {
      map: 'applet_auction_my_order',
      status: that.data.curStatus,
      page: page
    };
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(page);
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.orderList ? that.data.orderList : [];
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            orderList: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.orderList);
        } else {
          if (page <= 0) {
            that.setData({
              orderList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      showLoading: true,
      noMoretip: false,
      page: 0
    })
    that.requestOrderlist();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestOrderlist();
    }
  },
  toDetail:function(e){
    var dataset = e.currentTarget.dataset;
    var status = dataset.status;
    var gid = dataset.gid;
    var tid = dataset.tid ? dataset.tid:'';
    if (status == 0||status == 1){
      wx.navigateTo({
        url: '/subpages0/auctionpage/auctionDetail/auctionDetail?id=' + gid,
      })
    }else{
      wx.navigateTo({
        url: '/subpages0/auctionpage/orderDetail/orderDetail?tid=' + tid,
      })
    }
  },
  tologisticsInfo:function(e){
    var tid = e.currentTarget.dataset.tid;
    wx.navigateTo({
      url: '/subpages0/auctionpage/logisticsInfo/logisticsInfo?tid=' + tid
    })
  },
  tosubmitOrder: function (e) {
    var tid = e.currentTarget.dataset.tid;
    wx.navigateTo({
      url: '/subpages0/auctionpage/submitOrder/submitOrder?type=retainage&tid=' + tid
    })
  },
  delOrder: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.tid;
    wx.showModal({
      title: '删除提示',
      cancelText: '取消',
      confirmText: '确认',
      content: '确认删除该拍单吗？',
      confirmColor: '#1AAD16',
      success: function (res) {
        if (res.confirm) {
          //发起请求，获取列表列表
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.requestUrl,
            data: {
              map: 'applet_order_delete',
              tid: orderId
            },
            success: function (res) {
              if (res.data.ec == 200) {
                console.log(res.data.data);
                app.errorTip(that, res.data.data.msg, 2000);
                that.onPullDownRefresh();
              } else {
                app.errorTip(that, res.data.em, 2000);
              }
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  confirmReceiving: function (e) { //触发订阅消息
    var that = this;
    var curTid = e.currentTarget.dataset.tid;
    that.setData({
      curTid: curTid
    })
    app.getSubId(that, 'applet_order_confirm_accept', 'toConfirmReceiving');
  },
  toConfirmReceiving: function (e) {
    var that = this;
    var orderId = that.data.curTid;
    wx.showModal({
      title: '',
      cancelText: '还没收到',
      confirmText: '确认收货',
      content: '确认收到货物了吗？',
      confirmColor: '#1AAD16',
      success: function (res) {
        if (res.confirm) {
          //发起请求，获取列表列表
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.requestUrl,
            data: {
              map: 'applet_order_confirm_accept',
              tid: orderId
            },
            success: function (res) {
              if (res.data.ec == 200) {
                console.log(res.data.data);
                app.errorTip(that, res.data.data.msg, 2000);
                that.onPullDownRefresh();
              } else {
                app.errorTip(that, res.data.em, 2000);
              }
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})